/**
 * File name: ClusterTestDriver.java Author: Willard Wang Create date: Aug 24,
 * 2013
 */
package edu.sjtu.se.prophet.driver;

import java.util.Arrays;
import java.util.Collection;
import java.util.Random;

import edu.sjtu.se.prophet.cluster.Cluster;
import edu.sjtu.se.prophet.cluster.Clusterer;
import edu.sjtu.se.prophet.cluster.MatrixArrayDistanceMeter;
import edu.sjtu.se.prophet.cluster.PointWithId;
import edu.sjtu.se.prophet.cluster.InvalidClusterNumberException;
import edu.sjtu.se.prophet.cluster.KMedoids;
import edu.sjtu.se.prophet.cluster.MatrixMapDistanceMeter;
import edu.sjtu.se.prophet.cluster.Point;
import edu.sjtu.se.prophet.cluster.SimpleFastMedoids;

/**
 * @author deathspeeder
 * 
 */
public class ClusterTestDriver {

    /**
     * @param args
     */
    public static void main(String[] args) {
        test1();
        test2();

    }

    /**
     * 
     */
    private static void test1() {
        Point[] points = new Point[5];
        for (int i = 0; i < 5; i++) {
            points[i] = new PointWithId(new Integer(i).toString());
        }
        byte[][] distance = new byte[][] {
                //
                new byte[] { 0, 1, 2, 100, 120 },
                //
                new byte[] { 1, 0, 3, 120, 100 },
                //
                new byte[] { 2, 3, 0, 90, 100 },
                //
                new byte[] { 100, 120, 90, 0, 2 },
                //
                new byte[] { 120, 100, 100, 2, 0 } };

        MatrixMapDistanceMeter meter = new MatrixMapDistanceMeter(points, distance);
        Clusterer clusterer = new KMedoids();
        //Clusterer clusterer = new SimpleFastMedoids();
        try {
            Collection<Cluster> clusters = clusterer.cluster(
                    Arrays.asList(points), meter, 2);
            for (Cluster c : clusters) {
                System.out.print("Cluster:");
                for (Point p : c.getPoints())
                    System.out.print(" " + p);
                System.out.println();
            }
        } catch (InvalidClusterNumberException e) {
            e.printStackTrace();
        }
    }

    /**
     * 
     */
    private static void test2() {
        int NUM = 2000;
        Point[] points = new Point[NUM];
        for (int i = 0; i < NUM; i++) {
            points[i] = new PointWithId(new Integer(i).toString());
        }
        byte[][] distance = new byte[NUM][NUM];
        for (int i = 0; i < NUM; i++)
            for (int j = i; j < NUM; j++) {
                if (i == j)
                    distance[i][j] = 0;
                else {
                    byte ran = (byte) new Random().nextInt(Byte.MAX_VALUE);
                    distance[j][i] = ran;
                    distance[i][j] = ran;
                }
            }

        // for (int i = 0; i < NUM; i++) {
        // for (int j = 0; j < NUM; j++) {
        // System.out.print(" " + distance[i][j]);
        // }
        // System.out.println();
        // }

        MatrixMapDistanceMeter meterMap = new MatrixMapDistanceMeter(points, distance);
        MatrixArrayDistanceMeter meterArray = new MatrixArrayDistanceMeter(points, distance);
        Clusterer kClusterer = new KMedoids();
        try {
            long start = System.currentTimeMillis();
            Collection<Cluster> clusters = kClusterer.cluster(
                    Arrays.asList(points), meterMap, 10);
            long end = System.currentTimeMillis();
            System.out.println("Time: " + (end - start));
            // for (Cluster c : clusters) {
            // System.out.print("Cluster:");
            // for (Point p : c.getPoints())
            // System.out.print(" " + p);
            // System.out.println();
            // }

            kClusterer = new KMedoids();
            start = System.currentTimeMillis();
            clusters = kClusterer.cluster(Arrays.asList(points), meterArray, 10);
            end = System.currentTimeMillis();
            System.out.println("Time: " + (end - start));
        } catch (InvalidClusterNumberException e) {
            e.printStackTrace();
        }
    }

}
